﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AsmShaderConverter
{
    internal class InformationExportedShaders
    {
        class Row : IComparable
        {
            public string materialName;
            public readonly string[] samplerNames = new string[16];
            public readonly string[] samplerTypes = new string[16];


            public int CompareTo(object obj)
            {
                Row row2 = obj as Row;
                return string.Compare(this.materialName, row2.materialName);
            }
        }

        List<Row> rows;

        public InformationExportedShaders()
        {
            this.rows = new List<Row>();
        }
        public void AddShaderInformations(string materialName, List<HlslShader.Sampler.HlslSampler> sampler)
        {

            Row row = new Row();
            row.materialName = materialName;

            foreach (var item in sampler)
            {
                row.samplerNames[item.Register] = item.ExportName;
                row.samplerTypes[item.Register] = item.Sampler.ToString();
            }

            this.rows.Add(row);
        }

        public void SaveToFile(string filename)
        {
            if (this.rows.Count > 0)
            {
                StreamWriter sw = new StreamWriter(filename);
                int initColumnLength = 5;
                // Get Maximum Length for each coulumn
                int[] columnLengths = new int[16];
                for (int i = 0; i < columnLengths.Length; i++)
                    columnLengths[i] = initColumnLength;

                int columnWidtMaterialname = 5;
                rows.Sort();


                foreach (Row row in this.rows)
                {
                    columnWidtMaterialname = Math.Max(row.materialName.Length + 3, columnWidtMaterialname);

                    for (int i = 0; i < row.samplerNames.Length; i++)
                    {
                        if (row.samplerNames != null)
                        {
                            if (row.samplerNames[i] != null)
                            {
                                columnLengths[i] = Math.Max(row.samplerNames[i].Length + 3, columnLengths[i]);
                                columnLengths[i] = Math.Max(row.samplerTypes[i].Length + 3, columnLengths[i]);
                            }
                        }
                    }
                }

                //// Write to Stream
                //string format = "{0,- " + (columnWidtMaterialname + 1) + "}";
                //int index = 1;
                //foreach (int columnLength in columnLengths)
                //{
                //    format += "{" + index.ToString() + "," + (columnLength + 1).ToString() + "}";
                //}
                sw.Write("Name".PadRight(columnWidtMaterialname));
                sw.Write("S0".PadRight(columnLengths[0]));
                sw.Write("S1".PadRight(columnLengths[1]));
                sw.Write("S2".PadRight(columnLengths[2]));
                sw.Write("S3".PadRight(columnLengths[3]));
                sw.Write("S4".PadRight(columnLengths[4]));
                sw.Write("S5".PadRight(columnLengths[5]));
                sw.Write("S6".PadRight(columnLengths[6]));
                sw.Write("S7".PadRight(columnLengths[7]));
                sw.Write("S8".PadRight(columnLengths[8]));
                sw.Write("S9".PadRight(columnLengths[9]));
                sw.Write("S10".PadRight(columnLengths[10]));
                sw.Write("S11".PadRight(columnLengths[11]));
                sw.Write("S12".PadRight(columnLengths[12]));
                sw.Write("S13".PadRight(columnLengths[13]));
                sw.Write("S14".PadRight(columnLengths[14]));
                sw.WriteLine("S15".PadRight(columnLengths[15]));
                sw.WriteLine();

                foreach (Row row in this.rows)
                {
                    sw.Write(row.materialName.PadRight(columnWidtMaterialname));
                    int index = 0;

                    foreach (var item in row.samplerNames)
                    {

                        string samplerName = (row.samplerNames[index] == null) ? "----" : row.samplerNames[index];

                        sw.Write(samplerName.PadRight(columnLengths[index]));
                        index++;
                    }

                    sw.WriteLine();
                }

                sw.Close();
            }
            else
                MessageBox.Show("No Shader information avaible!");
        }

        internal void Clear()
        {
            this.rows.Clear();
        }
    }
}
